package com.kis.bot.main

import com.kis.bot.enums.StringEnums
import com.kis.bot.model.WeChat
import com.kis.bot.utils.api.LoginApi
import com.kis.bot.utils.api.OperApi
import com.squareup.okhttp.FormEncodingBuilder
import com.squareup.okhttp.OkHttpClient
import com.squareup.okhttp.Request
import java.awt.Desktop
import java.awt.Toolkit
import java.io.File
import java.io.FileOutputStream
import java.util.*

/**
 * Created by wongloong on 16-8-31.
 */
fun main(args: Array<String>) {
    val obj = WeChat()
    while (true) {
        LoginApi.createQrCode(obj)
        println("启动中")
        if (LoginApi.checkLogin(obj)) {
            println("扫描成功，请点击登录")
        }
        if (LoginApi.checkLogin(obj, 0)) {
            print("登录成功")
            break
        }
    }

    if (!LoginApi.login(obj)) {
        print("登录拉取信息过程出错")
    }
    OperApi.initWx(obj)

}

fun test() {
    System.setProperty("jsse.enableSNIExtension", "false")
    val client = OkHttpClient()
    val form = FormEncodingBuilder()
    form.add("t", "webwx")
    form.add("_", Date().time.toString())
    val request = Request.Builder().url(StringEnums.SCANURL.value + "/" + LoginApi.getUuid()).post(form.build()).addHeader("User-agent", StringEnums.USERAGENT.value).build()
    println(StringEnums.SCANURL.value + LoginApi.getUuid())
    val response = client.newCall(request).execute()
    if (response.isSuccessful) {
        println(response.body().toString())
        val pic = response.body().byteStream()
        val qrcode = File("${System.getProperty("user.dir")}/qr.jpeg")
        val output = FileOutputStream(qrcode)
        output.write(pic.readBytes())
    } else {
        println("服务器错误${response}")
    }
}